//
//  ViewController.swift
//  swift-demo
//
//  Created by ZT on 2023/4/18.
//

import UIKit

class ViewController: UIViewController, PlateManagerDelegate, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
 
    
    
    let PLATE_AUTH_CODE = "7332DBAFD2FD18301EF6"
    // 扫描识别按钮
    var videoScanButton: UIButton!
    
    // 导入识别按钮
    var importScanButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.white
        
        // 设置界面
        setupUI()
    }
    
    // 设置 UI
    func setupUI() {
        // 视频扫描按钮
        videoScanButton = UIButton(type: .system)
        videoScanButton.frame = CGRect(x: 50, y: 100, width: 200, height: 50)
        videoScanButton.setTitle("扫描识别", for: .normal)
        videoScanButton.addTarget(self, action: #selector(startVideoScan), for: .touchUpInside)
        view.addSubview(videoScanButton)
        
        // 导入扫描按钮
        importScanButton = UIButton(type: .system)
        importScanButton.frame = CGRect(x: 50, y: 200, width: 200, height: 50)
        importScanButton.setTitle("导入识别", for: .normal)
        importScanButton.addTarget(self, action: #selector(importScan), for: .touchUpInside)
        view.addSubview(importScanButton)
    }
    
    // 视频扫描按钮点击事件
    @objc func startVideoScan() {
        
        let plateManager = PlateManager.shared()
        
        plateManager.delegate = self
        
        plateManager.recognizePlateByVideoStream(with: self, usePush: false, isHorizontalRecognize: false, andAuthCode: PLATE_AUTH_CODE)
        
    }
    
    // 导入扫描按钮点击事件
    @objc func importScan() {
        // 调用导入扫描功能
        
        albumButtonClick()
    }
    
    // 相册按钮点击事件
    @objc func albumButtonClick() {
        // 判断相册资源是否可用
        let isPhotoLibraryAvailable = UIImagePickerController.isSourceTypeAvailable(.photoLibrary)
        
        if isPhotoLibraryAvailable {
            let controller = UIImagePickerController()
            controller.sourceType = .photoLibrary
            controller.mediaTypes = [kUTTypeImage as String]  // 只选择图片
            controller.delegate = self
            controller.modalPresentationStyle = .fullScreen
            // 打开相册
            self.present(controller, animated: true, completion: nil)
        } else {
            // 处理无法打开相册的情况
            print("相册不可用")
        }
    }
    
    // 相册选择图片回调
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        picker.dismiss(animated: true)
        
        if let originalImage = info[.originalImage] {
            
            let plateManager = PlateManager.shared()
                // 在 Swift 文件中调用
            plateManager.delegate = self
                
                // 设置是否识别摩托车
            plateManager.recognizePlate(withPhoto: originalImage as! UIImage, andAuthCode: PLATE_AUTH_CODE)
            
        }
        
    }
    
    // 相册选择图片取消回调
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        // 取消选择时关闭相册
        picker.dismiss(animated: true, completion: nil)
    }
    
    func platePhotoRecognizeFinish(withResult plateStr: String, plateColor: String, srcImage: UIImage, plateImage: UIImage, andErrorCode errorCode: Int32) {
        print("车牌--" + plateStr)
    }
    
    func cameraController(_ cameraController: UIViewController, videoStreamRecognizePlateFinishWithResult plateStr: String, plateColor: String, srcImage: UIImage, areaCut areaCutImage: UIImage, andPlateImage plateImage: UIImage) {
        cameraController.dismiss(animated: true)
        print("车牌--" + plateStr)
    }

    
}

